package com.wjq.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * ClassName: com.wjq.boot.config
 * Datetime: 2024/8/2 下午8:09
 * Description:
 *      跨域请求的配置类
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
@Configuration
public class CrossOriginRequestsConfig {

    @Bean
    CorsFilter getCorsFilter(){
        UrlBasedCorsConfigurationSource  source = new UrlBasedCorsConfigurationSource();
        // 配置拦截规则
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许一下域名进行跨域请求
        configuration.addAllowedOrigin("http://localhost");
        configuration.addAllowedOrigin("http://192.168.153.1");
        configuration.addAllowedOrigin("http://192.168.80.1");
        configuration.addAllowedOrigin("http://10.25.128.165");
//        configuration.addAllowedOrigin("*");
        configuration.addAllowedMethod("*");
        configuration.addAllowedHeader("*");
        // 注册跨域请求拦截器
        source.registerCorsConfiguration("/**",configuration);
        return new CorsFilter(source);
    }
}
